package com.jiawa.train.batch.config;


import jakarta.annotation.Resource;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;

@Configuration
public class SchedulerConfig {

    @Resource
    private MyJobFactory myJobFactory;

    @Bean
    public Scheduler scheduler(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setDataSource(dataSource);
        factory.setJobFactory(myJobFactory);
        factory.setStartupDelay(2); // 延迟启动
        factory.afterPropertiesSet(); // 关键！触发初始化
        return factory.getObject(); // 返回实际的 Scheduler 实例
    }
}